package com.cn.removewatermark.gui.controller;

import com.cn.removewatermark.batchdownload.BatchDownload;
import com.cn.removewatermark.batchdownload.Download;
import com.cn.removewatermark.parsingurl.ParsingURLs;
import javafx.application.Platform;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

import java.io.IOException;
import java.net.URL;
import java.util.*;

public class Show implements Initializable {

    @FXML
    private ScrollPane batchDownloadProgress;

    @FXML
    private TextField shareLink;

    @FXML
    private ProgressBar downloadProgress;

    @FXML
    private TextField userId;

    //当个视频下载对象
    private ParsingURLs parsingURLs = new ParsingURLs();

    //下载视频主页
    private BatchDownload batchDownload = new BatchDownload();

    /*
     * 批量下载视频
     * */
    @FXML
    void batchDown(ActionEvent event) {
        new Thread(() -> {
            batchDownload.start(userId.getText().trim());
        }).start();
        batchProgress();
    }

    /*
     * 单个视频下载
     * */
    @FXML
    void downOne(ActionEvent event) {
        try {
            ParsingURLs.progress = 0;
            parsingURLs.kuaiShouParsingUrl(shareLink.getText());
            updateProgress();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }

    /*
     *单个更新进度条
     * */
    public void updateProgress() {
        Service service = new Service() {
            @Override
            protected Task createTask() {
                return new Task() {
                    @Override
                    protected Void call() throws Exception {
                        while (true) {
                            downloadProgress.setProgress(ParsingURLs.progress);
                            if (ParsingURLs.progress == 1) {
                                break;
                            }
                            Thread.sleep(100);
                        }
                        return null;
                    }
                };
            }
        };
        service.start();
    }

    /*
     * 批量动态生成进度条，并且动态更新进度条
     * */
    public void batchProgress() {
        VBox vBox = new VBox();
        vBox.setPrefWidth(336);
        vBox.setPrefHeight(175.0);
        vBox.setStyle("-fx-background-color: #ff0000;");
        vBox.setAlignment(Pos.CENTER);
        Label label = new Label("正在下载中...");
        label.setPrefWidth(336);
        label.setText(label.getText());
        label.setFont(new Font(24));
        label.setAlignment(Pos.CENTER);
        vBox.getChildren().add(label);
        batchDownloadProgress.setContent(vBox);
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                if (null!=BatchDownload.countDownLatch) System.out.println(BatchDownload.countDownLatch.getCount());;
                if (null!=BatchDownload.countDownLatch&&BatchDownload.countDownLatch.getCount() == 0) {
                    Platform.runLater(() -> label.setText("下载完成"));
                    this.cancel();
                }
            }
        };
        timer.schedule(task, 500, 1000);
    }




}

